<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Getting started</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../boost_typeindex.html" title="Chapter 39. Boost.TypeIndex 4.1">
<link rel="prev" href="../boost_typeindex.html" title="Chapter 39. Boost.TypeIndex 4.1">
<link rel="next" href="config.html" title="Configuring and building the library">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../boost_typeindex.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeindex.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="config.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_typeindex.getting_started"></a><a class="link" href="getting_started.html" title="Getting started">Getting started</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="getting_started.html#boost_typeindex.getting_started.how_to_use">How to use</a></span></dt>
<dt><span class="section"><a href="getting_started.html#boost_typeindex.getting_started.example_with_boost_any">Example
      with Boost.Any</a></span></dt>
<dt><span class="section"><a href="getting_started.html#boost_typeindex.getting_started.example_with_boost_variant">Example
      with Boost.Variant</a></span></dt>
</dl></div>
<p>
      <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1acd639a0640fcc187eb762567a855b517.html" title="Type definition type_info">boost::typeindex::type_info</a></code>
      is a drop-in replacement for <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span></code>
      and <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1a92396e4950b1278f4df4b0919f3168be.html" title="Type definition type_index">boost::typeindex::type_index</a></code>
      is a drop-in replacement for <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_index</span></code>.
      Unlike Standard Library versions those classes can work without RTTI.
    </p>
<p>
      <code class="computeroutput"><span class="identifier">type_index</span></code> provides the full
      set of comparison operators, hashing functions and ostream operators, so it
      can be used with any container class.
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeindex.getting_started.how_to_use"></a><a class="link" href="getting_started.html#boost_typeindex.getting_started.how_to_use" title="How to use">How to use</a>
</h3></div></div></div>
<p>
        To start using Boost.TypeIndex:
      </p>
<div class="informaltable">
<a name="boost_typeindex.getting_started.how_to_use.porting"></a><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Replace this:
                </p>
              </th>
<th>
                <p>
                  With the following:
                </p>
              </th>
<th>
                <p>
                  More Info
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
<pre class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">typeinfo</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">typeindex</span><span class="special">&gt;</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
              </td>
<td>
                <p>
                  <code class="computeroutput"><a class="link" href="../boost_typeindex_header_reference.html#doxygen.boost_typeindex_header_reference.type__index_8hpp" title="Header &lt;boost/type_index.hpp&gt;">more... </a></code>
                </p>
              </td>
</tr>
<tr>
<td>
<pre class="table-programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_index</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_index</span>
</pre>
              </td>
<td>
                <p>
                  <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1a92396e4950b1278f4df4b0919f3168be.html" title="Type definition type_index">more... </a></code>
                </p>
              </td>
</tr>
<tr>
<td>
<pre class="table-programlisting"><span class="keyword">typeid</span><span class="special">(</span><span class="identifier">T</span><span class="special">)</span>
<span class="keyword">typeid</span><span class="special">(</span><span class="identifier">T</span><span class="special">).</span><span class="identifier">name</span><span class="special">()</span> <span class="comment">// not human readable</span>
<span class="keyword">typeid</span><span class="special">(</span><span class="identifier">variable</span><span class="special">)</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;().</span><span class="identifier">pretty_name</span><span class="special">()</span> <span class="comment">// human readable</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id_runtime</span><span class="special">(</span><span class="identifier">variable</span><span class="special">)</span>
</pre>
              </td>
<td>
                <p>
                  <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1aaa8536b5c80315f205036a85a9e8e3e2.html" title="Function template type_id">more... </a></code>
                </p>
                <p>
                  <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1a92396e4950b1278f4df4b0919f3168be.html" title="Type definition type_index">more... </a></code>
                </p>
                <p>
                  <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1ae179d82aeceebc66d4fe38985bdb9e82.html" title="Function template type_id_runtime">more...
                  </a></code>
                </p>
              </td>
</tr>
<tr>
<td>
<pre class="table-programlisting"><span class="comment">// attempt to save const, volatile, reference</span>
<span class="keyword">typeid</span><span class="special">(</span><span class="identifier">please_save_modifiers</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;)</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="comment">// cvr = const, volatile, reference</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id_with_cvr</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;()</span>
</pre>
              </td>
<td>
                <p>
                  <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1a15beb7454116a3e5aa29f30cbaad5315.html" title="Function template type_id_with_cvr">more...
                  </a></code>
                </p>
              </td>
</tr>
<tr>
<td>
<pre class="table-programlisting"><span class="comment">// when reference to `std::type_info` is required</span>
<span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">&amp;</span> <span class="identifier">v1</span> <span class="special">=</span> <span class="keyword">typeid</span><span class="special">(</span><span class="keyword">int</span><span class="special">);</span>

<span class="comment">// other cases</span>
<span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">*</span> <span class="identifier">v2</span> <span class="special">=</span> <span class="special">&amp;</span><span class="keyword">typeid</span><span class="special">(</span><span class="keyword">int</span><span class="special">);</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">&amp;</span> <span class="identifier">v1</span>
    <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;().</span><span class="identifier">type_info</span><span class="special">();</span>

<span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_index</span> <span class="identifier">v2</span>
    <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;();</span>
</pre>
              </td>
<td>
                <p>
                  <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1a92396e4950b1278f4df4b0919f3168be.html" title="Type definition type_index">more... </a></code>
                </p>
                <p>
                  <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1aaa8536b5c80315f205036a85a9e8e3e2.html" title="Function template type_id">more... </a></code>
                </p>
              </td>
</tr>
</tbody>
</table>
</div>
<p>
        If you are using <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1ae179d82aeceebc66d4fe38985bdb9e82.html" title="Function template type_id_runtime">type_id_runtime()</a></code>
        methods and RTTI is disabled, make sure that classes that are passed to
        <code class="computeroutput"><span class="identifier">type_id_runtime</span><span class="special">()</span></code>
        are marked with <code class="computeroutput"><a class="link" href="../doxygen/boost_typeindex_header_reference/type__index_8hpp_1aa95e33fb9aa2d570e580cfbde489f6c6.html" title="Macro BOOST_TYPE_INDEX_REGISTER_CLASS">BOOST_TYPE_INDEX_REGISTER_CLASS</a></code>
        macro.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeindex.getting_started.example_with_boost_any"></a><a class="link" href="getting_started.html#boost_typeindex.getting_started.example_with_boost_any" title="Example with Boost.Any">Example
      with Boost.Any</a>
</h3></div></div></div>
<p>
        Here is how TypeIndex could be used in <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span></code>:
      </p>
<div class="informaltable">
<a name="boost_typeindex.getting_started.example_with_boost_any.any"></a><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Before
                </p>
              </th>
<th>
                <p>
                  With TypeIndex
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
<pre class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">typeinfo</span><span class="special">&gt;</span></pre>
              </td>
<td>
<pre class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></pre>
              </td>
</tr>
<tr>
<td>
<pre class="table-programlisting"><span class="keyword">virtual</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span> <span class="special">&amp;</span> <span class="identifier">type</span><span class="special">()</span> <span class="keyword">const</span> <span class="identifier">BOOST_NOEXCEPT</span>
<span class="special">{</span>
    <span class="comment">// requires RTTI</span>
    <span class="keyword">return</span> <span class="keyword">typeid</span><span class="special">(</span><span class="identifier">ValueType</span><span class="special">);</span>
<span class="special">}</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="keyword">virtual</span> <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_info</span> <span class="special">&amp;</span> <span class="identifier">type</span><span class="special">()</span> <span class="keyword">const</span> <span class="identifier">BOOST_NOEXCEPT</span>
<span class="special">{</span>
    <span class="comment">// now works even with RTTI disabled</span>
    <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">ValueType</span><span class="special">&gt;().</span><span class="identifier">type_info</span><span class="special">();</span>
<span class="special">}</span>
</pre>
              </td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeindex.getting_started.example_with_boost_variant"></a><a class="link" href="getting_started.html#boost_typeindex.getting_started.example_with_boost_variant" title="Example with Boost.Variant">Example
      with Boost.Variant</a>
</h3></div></div></div>
<p>
        Here is how TypeIndex could be used in <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">variant</span><span class="special">/</span><span class="identifier">variant</span><span class="special">.</span><span class="identifier">hpp</span></code>:
      </p>
<div class="informaltable">
<a name="boost_typeindex.getting_started.example_with_boost_variant.variant"></a><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Before
                </p>
              </th>
<th>
                <p>
                  With TypeIndex
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
<pre class="table-programlisting"><span class="preprocessor">#if</span> <span class="special">!</span><span class="identifier">defined</span><span class="special">(</span><span class="identifier">BOOST_NO_TYPEID</span><span class="special">)</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">typeinfo</span><span class="special">&gt;</span> <span class="comment">// for typeid, std::type_info</span>
<span class="preprocessor">#endif</span> <span class="comment">// BOOST_NO_TYPEID</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
              </td>
</tr>
<tr>
<td>
<pre class="table-programlisting"><span class="preprocessor">#if</span> <span class="special">!</span><span class="identifier">defined</span><span class="special">(</span><span class="identifier">BOOST_NO_TYPEID</span><span class="special">)</span>

<span class="keyword">class</span> <span class="identifier">reflect</span>
    <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">static_visitor</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">&amp;&gt;</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span> <span class="comment">// visitor interfaces</span>

    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;)</span> <span class="keyword">const</span> <span class="identifier">BOOST_NOEXCEPT</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="keyword">typeid</span><span class="special">(</span><span class="identifier">T</span><span class="special">);</span>
    <span class="special">}</span>

<span class="special">};</span>

<span class="preprocessor">#endif</span> <span class="comment">// BOOST_NO_TYPEID</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="keyword">class</span> <span class="identifier">reflect</span>
    <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">static_visitor</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">&amp;&gt;</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span> <span class="comment">// visitor interfaces</span>

    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;)</span> <span class="keyword">const</span> <span class="identifier">BOOST_NOEXCEPT</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;().</span><span class="identifier">type_info</span><span class="special">();</span>
    <span class="special">}</span>

<span class="special">};</span>
</pre>
              </td>
</tr>
<tr>
<td>
<pre class="table-programlisting"><span class="preprocessor">#if</span> <span class="special">!</span><span class="identifier">defined</span><span class="special">(</span><span class="identifier">BOOST_NO_TYPEID</span><span class="special">)</span>
    <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">&amp;</span> <span class="identifier">type</span><span class="special">()</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="identifier">detail</span><span class="special">::</span><span class="identifier">variant</span><span class="special">::</span><span class="identifier">reflect</span> <span class="identifier">visitor</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">apply_visitor</span><span class="special">(</span><span class="identifier">visitor</span><span class="special">);</span>
    <span class="special">}</span>
<span class="preprocessor">#endif</span>
</pre>
              </td>
<td>
<pre class="table-programlisting"><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_info</span><span class="special">&amp;</span> <span class="identifier">type</span><span class="special">()</span> <span class="keyword">const</span>
<span class="special">{</span>
    <span class="identifier">detail</span><span class="special">::</span><span class="identifier">variant</span><span class="special">::</span><span class="identifier">reflect</span> <span class="identifier">visitor</span><span class="special">;</span>
    <span class="keyword">return</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">apply_visitor</span><span class="special">(</span><span class="identifier">visitor</span><span class="special">);</span>
<span class="special">}</span>
</pre>
              </td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="copyright-footer">Copyright © 2012-2025 Antony
      Polukhin<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../boost_typeindex.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeindex.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="config.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
